package gen.desktop.components;

import gen.core.managers.ConnConfigManager;
import gen.core.models.ConnConfig;
import gen.core.utils.GenUtils;
import gen.desktop.Application;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ConnConfigDelDailog extends JDialog {
	private static final long serialVersionUID = 1L;
	private JComboBox comboBox;
	private JButton buttonCanecl;
	private JButton buttonDelete;

	public ConnConfigDelDailog() {
		getContentPane().setLayout(null);

		JLabel label = new JLabel("选择连接：");
		label.setBounds(29, 26, 70, 15);
		getContentPane().add(label);

		comboBox = new JComboBox();
		comboBox.setBounds(97, 23, 236, 21);
		getContentPane().add(comboBox);

		buttonDelete = new JButton("删除");
		buttonDelete.setBounds(109, 65, 60, 23);
		getContentPane().add(buttonDelete);

		buttonCanecl = new JButton("取消");
		buttonCanecl.setBounds(208, 65, 60, 23);
		getContentPane().add(buttonCanecl);

		init();
	}

	private void init() {
		int width = 380;
		int height = 150;

		// 居中
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds(((int) dim.getWidth() - width) / 2, ((int) dim.getHeight() - height) / 2, width, height);

		loadConnConfigListToComboBox();

		buttonDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int choice = JOptionPane.showOptionDialog(null, "确定要删除么？", "删除确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
				if (choice != JOptionPane.OK_OPTION) {
					return;
				}

				ConnConfigManager aConnConfigManager = new ConnConfigManager();
				Connection conn = null;

				try {
					conn = Application.getSqliteClient().createConnection();

					ConnConfig selectedConnConfig = (ConnConfig) comboBox.getSelectedItem();
					aConnConfigManager.delete(conn, selectedConnConfig.getId());

					conn.commit();
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(null, ex.getMessage(), "删除失败", JOptionPane.ERROR_MESSAGE);
					return;
				} finally {
					GenUtils.closeConnection(conn);
				}

				loadConnConfigListToComboBox();
			}
		});

		buttonCanecl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
	}

	private void loadConnConfigListToComboBox() {
		ConnConfigManager aConnConfigManager = new ConnConfigManager();
		Connection conn = null;
		try {
			conn = Application.getSqliteClient().createConnection();
			ConnConfig[] ConnConfigArray = aConnConfigManager.list(conn, Application.getOwner(), "Main");
			comboBox.setModel(new DefaultComboBoxModel(ConnConfigArray));
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, ex.getMessage(), "异常", JOptionPane.ERROR_MESSAGE);
			return;
		} finally {
			GenUtils.closeConnection(conn);
		}
	}

	private void close() {
		this.setVisible(false);
		this.dispose();
	}
}
